﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.ComponentModel;
using Kristall.Engine;

namespace Kristall
{
    public class SelectionManager : INotifyPropertyChanged
    {
        int selectionDepth = -1;
        List<AABB> selections = new List<AABB>();
        public event PropertyChangedEventHandler PropertyChanged;
        //public event EventHandler SelectedObjectChanged;
        AABB _selectedObject = null;
        public AABB SelectedObject
        {
            get
            {
                return _selectedObject;
            }
            internal set
            {
                _selectedObject = value;
                notifyPropertyChanged("SelectedObject");
            }
        }

        void notifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public void AddSelection(AABB select)
        {
            selections.Add(select);

            if (selections.Count == 1)
            {
                SetSelection(0);
            }
        }

        public List<AABB> GetSelections()
        {
            return new List<AABB>(selections);
        }

        public void ClearSelection()
        {
            selections.Clear();
            this.SetSelection(-1);
        }

        public bool HasAny()
        {
            return selections.Any();
        }

        public bool Contains(Point p)
        {
            if (SelectedObject != null)
            {
                return SelectedObject.Bounds.Contains(p);
            }
            return false;
        }

        public void RemoveCurrent()
        {
            if (SelectedObject != null)
            {
                this.selections.RemoveAt(selectionDepth);
                selectionDepth--;

                SetSelection(selectionDepth);
            }
        }

        public void NextSelection()
        {
            SetSelection(selectionDepth + 1);
        }

        public void PreviousSelection()
        {
            SetSelection(selectionDepth - 1);
        }

        public void SetSelection(int index)
        {
            AABB previousSelection = SelectedObject;

            if (selections.Any())
            {
                selectionDepth = (int)MathHelper.Clamp(index, 0, selections.Count - 1);
                SelectedObject = selections[selectionDepth];
            }
            else
            {
                SelectedObject = null;
            }

            //if (previousSelection != SelectedObject)
            //{
            //    SelectedObjectChanged(SelectedObject, null);
            //}
        }

        public void Select(AABB select)
        {
            if (selections.Contains(select))
            {
                this.SetSelection(selections.IndexOf(select));
            }
            else
            {
                this.AddSelection(select);
                this.SetSelection(selections.Count - 1);
            }
        }
    }
}
